<?php
namespace app\libraryadmin\controller;
use \think\Controller;
use \think\Db;
use \think\Session;
//use \Common;

class Book extends Common
{
	public function index()
	{
    $laid=Session::get('login_laid');
   // dump($laid);
		$rs=Db::query("select * from books b,booktype t, libraryadmin la  where b.tid=t.tid and b.laid=$laid and la.laid=b.laid order by b.bid desc");
		$this->assign("books",$rs);
		return $this->fetch();
	}
	public function addbook()
	{
		$rs=Db::name('booktype')->select();
		$this->assign("type",$rs);
		return $this->fetch();
	}
	public function doadd()
	{	
		$pic="";
		if(request()->isPost())
		{
			$file = request()->file('pic');
		//dump($file) ;

			 if($file){
        	 $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'bookimg');
        	 if ($info) {
        	 	$pic=$info->getFilename();
        	 }
        	 else{
        	 	echo $file->getError();
        	 }
		}
					$data=['bname'=>input('param.bname'),'tid'=>input("param.tid"),
					'laid'=>Session::get('login_laid'),'intro'=>input('param.intro'),'authorname'=>input("param.authorname"),'price'=>input("param.price"),'pic'=>"$pic"];
					//var_dump($data);
					
					$rs=Db::name("books")->insert($data);
				if($rs)
					$this->success("添加视频信息成功","index");
					else $this->error("添加视频信息失败");
		}
	}
	public function delete($bid)
	{
		$data=Db::name("books")->where("bid=$bid")->find();
		//var_dump($data);
		$rs=unlink("static/bookimg/{$data['pic']}");
		if(!$rs)
		{
           $this->error('删除图片失败');
		}
		else{
	if(Db::name("books")->delete($bid))
	{
		//echo Db::name("books")->getLastSql();exit;
		$this->success('删除成功');
	}else $this->error('删除失败');
	}
	}

  public function edit($bid)
  {
  	 $rs=Db::name("books")->where("bid=$bid")->find();
  	 $type=Db::name("booktype")->select();
  	 $this->assign("b",$rs);
  	 $this->assign("type",$type);
  	return $this->fetch();
  }
  public function doedit()
  {
  		$pic="";
      $data=[];
      $picname=null;
  	if(request()->isPost())
  	{
      $bid=input('param.bid');
  		$file=request()->file('pic');
  		if($file)
  		{
  			
  			$rs=Db::name('books')->where("bid=$bid")->find();
  			
  			 $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'bookimg');
        	 if ($info) {

        	 	$pic=$info->getFilename();
            
              $data=['pic'=>"$pic"];
              unlink("static/bookimg/{$rs['pic']}");
            //  dump($data);
        	 }
        	 else{
        	 	echo $file->getError();
        	 }
        	
  		}
  		 $data+=['bname'=>input('param.bname'),'tid'=>input("param.tid"),'intro'=>input('param.intro'),'authorname'=>input("param.authorname"),'price'=>input("param.price")];
  	//	var_dump($data);
  		$sql=Db::name("books")->where("bid=$bid")->update($data);
  		if($sql)
      {
        
  			$this->success("修改成功","index");
      }
  		else $this->error("修改失败");


  	}
  }

}